package ru.spb.osll.myfit.client.view;

import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.rpc.AsyncCallback;
import ru.spb.osll.myfit.client.dialog.ErrorDialog;
import ru.spb.osll.myfit.client.service.ChartsService;
import ru.spb.osll.myfit.client.service.ChartsServiceAsync;
import ru.spb.osll.myfit.common.chart.ChartTree;

public class ChartsViewController implements IViewController {
  private final ChartsServiceAsync m_service = GWT.create(ChartsService.class);
  private final ErrorDialog m_errorDialog = new ErrorDialog();
  private ChartsView m_chartsView = null;

  @Override
  public IView getView(String address) {
    m_chartsView = new ChartsView();

    m_service.getChartsTree(new AsyncCallback<ChartTree>() {
      @Override
      public void onFailure(Throwable caught) {
        m_errorDialog.setMessage(caught);
        m_errorDialog.center();
      }

      @Override
      public void onSuccess(ChartTree result) {
        m_chartsView.display(result);
      }
    });

    return m_chartsView;
  }
}
